//
//  YPMessageDetailViewController.m
//  ChengHuiTong
//
//  Created by KiVen on 15/11/25.
//  Copyright © 2015年 张文权. All rights reserved.
//

#import "YPMessageDetailViewController.h"
#import "YPStationMessageViewController.h"

@interface YPMessageDetailViewController ()<UIAlertViewDelegate>

@end

@implementation YPMessageDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    YPStationMessageBin * bin =[[YPStationMessageBin alloc]init];
    
    bin.token = [YPShareAccountInfo shareAccountInfo].token;
    bin.mid =self.midString;

    [YPHttpRequestMethod sendAsyRequestURL:CMD_STATION_MESSAGE_DETAIL withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
        
        NSLog(@"responseDictionary=%@",responseDictionary);

        if ([responseDictionary[@"status"] intValue]==0) {
            
            YPStationMessageModel *stationMessageModel = [[YPStationMessageModel alloc] initWithDictionary:responseDictionary];
            
            self.stationMessageModel = stationMessageModel;
            
            [self creatMessageDetailView];

        }
        

        
    } withFailedBlock:^(NSError *error) {
        
    } withIndicator:NO atView:self.view];
    
    
    
    

}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    [MobClick beginLogPageView:@"站内信详情"];

}
- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:YES];
    
    [MobClick endLogPageView:@"站内信详情"];
    
}
- (void)creatMessageDetailView{
    
    self.navigationItem.title = self.stationMessageModel.title;

    
    {
       UIButton * rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,20, 20)];
        [rightButton addTarget:self action:@selector(deleteBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [rightButton setBackgroundImage:[UIImage imageNamed:@"trash"] forState:UIControlStateNormal];
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        self.navigationItem.rightBarButtonItem = rightItem;
    }

    
    
    UILabel * contentLabel = [[UILabel alloc]init];
    //    contentLabel.numberOfLines =2;
    //    contentLabel.text = @"标编号CHT007559，红酒经销商资金周转（到期还本第三标）满标复审，投标资金扣除";
//    contentLabel.backgroundColor = [UIColor orangeColor];
    contentLabel.text = self.stationMessageModel.content;
    contentLabel.font = [UIFont systemFontOfSize:14];
    contentLabel.numberOfLines =0;

    CGSize maxSize = CGSizeMake(SCREEN_WIDTH-15-15,SCREEN_HEIGHT);
    
    CGSize contentSize = [self.stationMessageModel.content boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:contentLabel.font} context:nil].size;
    contentLabel.frame =CGRectMake(15, 10, SCREEN_WIDTH-15-15, contentSize.height);
    [self.view addSubview:contentLabel];
    
    
    UILabel * timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-15-120, CGRectGetMaxY(contentLabel.frame)+10, 120, 20)];
//    timeLabel.backgroundColor = [UIColor yellowColor];
    timeLabel.font = [UIFont systemFontOfSize:12];
    timeLabel.textAlignment = NSTextAlignmentRight;
    NSString *str = self.stationMessageModel.addtime;//时间戳
    //    NSTimeInterval time=[str doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
    NSTimeInterval time=[str doubleValue];//因为时差问题要加8小时 == 28800 sec
    
    timeLabel.text = [LGToolMethods stringFromInterval:time withFormate:@"yyyy-MM-dd"];
    

    [self.view addSubview:timeLabel];


    
}
- (void)deleteBtnClick{
    
    
    UIAlertView * deleteAlertView = [[UIAlertView alloc]initWithTitle:nil message:@"你确实要删除这条信息？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
    
    [deleteAlertView show];
    
    
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        
        NSLog(@"11111");
        
        
        YPStationMessageBin * bin =[[YPStationMessageBin alloc]init];
        
        bin.token = [YPShareAccountInfo shareAccountInfo].token;
        bin.mid =self.stationMessageModel.mid;

        
        [YPHttpRequestMethod sendAsyRequestURL:CMD_DELETE_STATION_MESSAGE withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
            
            NSLog(@"responseDictionary=%@",responseDictionary);
           
            if ([responseDictionary[@"status"] intValue]==0) {
                
                [self.navigationController popViewControllerAnimated:YES];
            }
            
            
        } withFailedBlock:^(NSError *error) {
            
            
        } withIndicator:YES atView:self.view];

        
        
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
